

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Furienson
 */
public class ServletAnuncio extends HttpServlet {
 
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String comando=request.getParameter("comando");
    if(null == comando || comando.equals("")){  
         response.sendRedirect("/ServletAnuncio?comando=listarAnuncio");
        
    }else if(comando.equals("insertarAnuncio")){
            int id=0;
            try {
                id=Integer.parseInt(request.getParameter("id"));
            } catch (NumberFormatException e) {
                request.getSession().setAttribute("Mensaje", "No se ha podido mostrar el anuncio");
                response.sendRedirect("/ServletAnuncio?comando=verAnuncio&id="+id+"&e=1");
            }
            Anuncio anuncio = new Anuncio();
            try {
                anuncio.getAnuncio(id);
                request.getSession().setAttribute("anuncio", anuncio);
            } catch (SQLException ex) {
                Logger.getLogger(ServletAnuncio.class.getName()).log(Level.SEVERE, null, ex);
            }
            request.getRequestDispatcher("verAnuncio.jsp").forward(request, response);
            
     }else if(comando.equals("insertarAnuncio")){
            
     }else if(comando.equals("listarAnuncios")){
            ListAnuncios listadoanuncios = new ListAnuncios();
            try {
                listadoanuncios.getAnuncios();
            } catch (SQLException ex) {
                Logger.getLogger(ServletAnuncio.class.getName()).log(Level.SEVERE, null, ex);
            }
       
     }else if(comando.equals("borrarAnuncio")){
           int id=0;
            try {
                id=Integer.parseInt(request.getParameter("id"));
            } catch (NumberFormatException e) {
                request.getSession().setAttribute("Mensaje", "No se ha podido borrar el anuncio");
                response.sendRedirect("/ServletAnuncio?comando=verAnuncio&id="+id+"&e=1");
            }
            Anuncio anuncio = new Anuncio();
            try {
                anuncio.deleteAnuncio(id);
            } catch (SQLException ex) {
                Logger.getLogger(ServletAnuncio.class.getName()).log(Level.SEVERE, null, ex);
            }
        }    
    }

   

}
